﻿
@{ ViewData["Title"] = "文章列表"; }
<blockquote class="layui-elem-quote quoteBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            <input type="text" name="Title" data-op="cn" class="layui-input searchVal" placeholder="请输入关键字" />
        </div>
        <a class="layui-btn search_btn" data-type="reload">搜索</a>
    </div>
    <div></div>
</blockquote>

<table id="laytable" lay-filter="articleList"></table>
@section script{
    <script type="text/javascript">
        layui.use(['tools', 'laytpl'], function () {
            let $ = layui.$, table = layui.table, tools = layui.tools, form = layui.form;

            table.render({
                elem: '#laytable',
                url: '/BlogManage/Article/Index',
                method: 'post',
                toolbar: '#toolsHead',
                loading: true,
                height: "full-125",
                limit: 15,
                limits: [10, 15, 20, 25, 30, 35, 40, 45, 50],
                page: true,
                where: { order: 'CreatorTime desc' },
                cols: [[
                    { type: 'numbers' },
                    {
                        field: 'Title', title: '标题', templet: function (d) {
                            return '<a class="layui-blue" title="' + d.Title + '" href="javascript:void(0)">' + d.Title + '</a>';
                        }
                    },
                    {
                        field: 'Source', title: '来源', templet: function (d) {
                            return d.CreativeType == 1 ? '【转载】<a class="layui-blue" target="_blank" href="' + d.SourceLink + '">' + d.Source + '</a>' : '【原创】';
                        }
                    },
                    {
                        field: 'Visible', title: '是否显示', templet: function (d) {
                            return '<input type="checkbox" name="enabled" lay-filter="enabled" lay-skin="switch" key="' + d.Id + '" lay-text="ON|OFF" ' + (d.Visible ? 'checked' : '') + '>'
                        }
                    }, {
                        field: 'IsTop', title: '是否置顶', templet: function (d) {
                            return '<input type="checkbox" name="top" lay-filter="top" lay-skin="switch" key="' + d.Id + '" lay-text="ON|OFF" ' + (d.IsTop ? 'checked' : '') + '>'
                        }
                    },
                    { field: 'Author', title: '作者' },
                    { field: 'ReadTimes', title: '阅读次数' },
                    { field: 'CreatorTime', title: '创建日期' },
                    { title: '操作', fixed: 'right', align: 'center', hide: rowButton.length == 0, width: rowButton.length * 80, templet: '#toolsRow' }
                ]]
            });

            table.on('tool(articleList)', function (obj) {
                var url = $(this).data("url"), layEvent = obj.event;
                active[layEvent](url, obj.data.Id);
            });

            table.on('toolbar(articleList)', function (obj) {
                var url = $(this).data("url");
                var layEvent = obj.event;
                active[layEvent](url);
            });

            table.on('sort(articleList)', function (obj) {
                table.reload("laytable",
                    {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        },
                        where: {
                            //排序字段
                            order: obj.field + " " + obj.type //排序方式

                        }
                    });
            });

            form.on('switch(enabled)', function (data) {
                let _this = $(this), s = data.elem.checked;
                tools.submitConfrim({
                    prompt: "您确定需要" + (s ? "显示" : "隐藏") + "当前文章吗？",
                    url: "/BlogManage/Article/Show",
                    data: { id: _this.attr("key"), status: s },
                    cancelCallBack: true,
                    success: function (result) {
                        if (result.StatusCode === 200) {
                            _this.prop('checked', s);
                        } else {
                            _this.prop('checked', !s);
                        }
                        form.render("checkbox");
                    }, cancel: function () {
                        _this.prop('checked', !s);
                        form.render("checkbox");
                    }
                });
            });
            form.on('switch(top)', function (data) {
                let _this = $(this), s = data.elem.checked;
                tools.submitConfrim({
                    prompt: "您确定需要" + (s ? "置顶" : "取消置顶") + "当前文章吗？",
                    url: "/BlogManage/Article/Top",
                    data: { id: _this.attr("key"), status: s },
                    cancelCallBack: true,
                    success: function (result) {
                        if (result.StatusCode === 200) {
                            _this.prop('checked', s);
                        } else {
                            _this.prop('checked', !s);
                        }
                        form.render("checkbox");
                    }, cancel: function () {
                        _this.prop('checked', !s);
                        form.render("checkbox");
                    }
                });
            });

            $(".search_btn").click(function () {
                table.reload("laytable", {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: tools.condition()
                });
            });

            var active = {
                add: function (url) {
                    tools.moduleOpen({
                        title: "新增文章",
                        url: url,
                        width: "100%",
                        height: "100%",
                        callBack: function (index, layero) {
                            var iframeWindow = window["layui-layer-iframe" + index];
                            iframeWindow.layui.form.on("submit(save)", function (data) {
                                data.field.IsTop = data.field.IsTop == 1;
                                data.field.Visible = data.field.Visible == 1;
                                data.field.Tags = data.field.Tags.split(",");
                                data.field.Categories = data.field.Categories.split(",");
                                tools.submit({
                                    url: url,
                                    data: data.field,
                                    loading: "正在保存数据...",
                                    success: function () {
                                        table.reload("laytable");//重新加载
                                        layer.close(index);
                                    }
                                });
                            });
                            layero.find("iframe").contents().find("#btnSave").trigger("click");
                        }
                    });
                },
                edit: function (url, id) {
                    tools.moduleOpen({
                        title: "编辑文章",
                        url: url + "?key=" + id,
                        width: "100%",
                        height: "100%",
                        callBack: function (index, layero) {
                            let iframeWindow = window["layui-layer-iframe" + index];
                            iframeWindow.layui.form.on("submit(save)", function (data) {
                                data.field.IsTop = data.field.IsTop == 1;
                                data.field.Visible = data.field.Visible == 1;
                                data.field.Tags = data.field.Tags.split(",");
                                data.field.Categories = data.field.Categories.split(",");
                                if (data.field.PublishDate == "") {
                                    data.field.PublishDate = data.field.CreatorTime;
                                }
                                tools.submit({
                                    url: url + "?Id=" + id,
                                    data: data.field,
                                    loading: "正在保存数据...",
                                    success: function () {
                                        table.reload("laytable");//重新加载
                                        layer.close(index);
                                    }
                                });
                            });
                            layero.find("iframe").contents().find("#btnSave").trigger("click");
                        }
                    });
                },
                delete: function (url, id) {
                    tools.submitConfrim({
                        prompt: "您确定需要删除当前文章吗？",
                        url: url,
                        data: { key: id },
                        success: function () {
                            table.reload("laytable");//重新加载
                        }
                    });
                }
            };
        });
    </script>
}

